//
//  LineView.m
//  视频播放
//
//  Created by zhangzhenyun on 2018/8/3.
//  Copyright © 2018年 Beijing Youjiu Network. All rights reserved.
//

#import "LineView.h"


@interface LineView () {
    CGFloat index;
}
@property (nonatomic, strong) NSMutableArray *lineArr;
@property (nonatomic, strong) UIView *containerView1;
@property (nonatomic, strong) UIView *containerView2;
@property (nonatomic, assign) CGFloat lineSpace;
@property (nonatomic, assign) CGFloat lineW;


@end


@implementation LineView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _lineSpace = Fix375(5);
        _lineW = Fix375(2);
        [self setupViews];
    }
    return self;
}
#pragma mark -
- (void)setupViews {
    self.lineArr = [NSMutableArray array];
//    self.backgroundColor = [UIColor greenColor];
    [self addSubview:self.containerView1];
    [self addSubview:self.containerView2];
}
#pragma mark - setter
-(void)setVolum:(int)volum {
    _volum = abs(volum);
    if (_volum < 2.4) {
        _volum = 2.4;
    }
    
    index ++;

    CAShapeLayer *lineLayer = [self creatLineLayer];
    
    CGFloat lineHeight = (CGFloat)_volum / 30.0 * Fix375(90);
    CGFloat lineTop = Fix375(45) - lineHeight / 2;
    CGFloat lineBottom =  Fix375(45) + lineHeight / 2;
    CGFloat x = index == 0 ? self.lineW / 2 : self.lineW / 2 +  (index - 1) * (self.lineSpace + self.lineW);
    x = SCREEN_WIDTH - x;
    UIGraphicsBeginImageContext(self.frame.size);

    UIBezierPath *path = [self creatLinePathX:x top:lineTop bottom:lineBottom];
    lineLayer.path = path.CGPath;
    
    UIGraphicsEndImageContext();

    [self.containerView1.layer addSublayer:lineLayer];

//    [self.lineArr addObject:lineLayer];
    
    self.containerView1.x = -x;
}

- (CAShapeLayer *)creatLineLayer {

    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineCap = kCALineCapButt;
    lineLayer.lineJoin = kCALineJoinRound;
    lineLayer.fillColor = [[UIColor whiteColor] CGColor];
    lineLayer.strokeColor = [[UIColor whiteColor] CGColor];
    lineLayer.lineWidth = self.lineW;
    
    return lineLayer;
}
- (UIBezierPath *)creatLinePathX:(CGFloat)x top:(CGFloat)top bottom:(CGFloat)bottom {
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(x, top)];
    [path addLineToPoint:CGPointMake(x, bottom)];
    
    return path;
}
#pragma mark - getter
-(UIView *)containerView1 {
    if (_containerView1 == nil) {
        _containerView1 = [[UIView alloc] initWithFrame:CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH, Fix375(90))];
    }
    return _containerView1;
}
-(UIView *)containerView2 {
    if (_containerView2 == nil) {
        _containerView2 = [[UIView alloc] initWithFrame:CGRectMake(-SCREEN_WIDTH, 0, SCREEN_WIDTH, Fix375(90))];
    }
    return _containerView2;
}
@end
